package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-13 14:44
 **/
public class LeetCode74 {

    /*public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length, n = matrix[0].length;
        int low = 0, high = m * n - 1;
        while (low <= high) {
            int mid = (high - low) / 2 + low;
            int x = matrix[mid / n][mid % n];
            if (x < target) {
                low = mid + 1;
            } else if (x > target) {
                high = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }*/


    public static boolean searchMatrix(int[][] matrix, int target) {
        if (matrix.length == 0) {
            return false;
        }
        int m = matrix.length, n = matrix[0].length;
        int low = 0, high = m * n - 1;
        while (low <= high) {
            //移位运算符优先级小于+号，所以前面要加括号
            int mid = (high + low) >> 1;
            int x = matrix[mid / n][mid % n];
            if (x == target) {
                return true;
            } else if (x > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return false;
    }

    /*public static boolean searchMatrix(int[][] matrix, int target) {
        int i = matrix.length - 1;
        //表示数组的列数
        int j = matrix[0].length - 1;
        return binarySearch(matrix, 0, 0, i, j, target);
    }*/

    public static boolean binarySearch(int[][] matrix, int startX, int startY, int endX, int endY, int target) {
        //查找中心位置的索引，即[x,y]
        int row = matrix.length;
        int col = matrix[0].length;
        int location = ((endX * col + endY) + (startX * col + startY)) >> 1;
        int x = location / col;
        int y = location % col;
        if (matrix[x][y] == target) {
            return true;
        }
        if (startX == endX && startY == endY) {
            return false;
        }

        if (matrix[x][y] > target) {
            if (startX == x && startY == y) {
                return false;
            }
            if (y == 0) {
                //[0][0]>target,说明target不存在于数组中
                if (x == 0) {
                    return false;
                }

                return binarySearch(matrix, startX, startY, x - 1, col - 1, target);
            }
            return binarySearch(matrix, startX, startY, x, y - 1, target);
        }
        if (y == col - 1) {
            //[col][col]<target,说明target不存在于数组中
            if (x == row - 1) {
                return false;
            } else {
                return binarySearch(matrix, x + 1, 0, endX, endY, target);
            }
        }
        return binarySearch(matrix, x, y + 1, endX, endY, target);
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        //int[][] matrix = {{1},{3}};
        //int[][] matrix = {{1},{3},{5}};
        //int[][] matrix = {{1,1},{3,3}};
        //boolean b = searchMatrix(matrix, 3);
        //System.out.println("b = " + b);
        searchMatrix(matrix, 3);
    }
}
